function Parent() { }
function Child() { }
Child.prototype.__proto__ = Parent.prototype;
const obj = new Child();
console.log(obj instanceof Child);
// true
console.log(obj instanceof Parent);
// true复制代码



function Parent() { this.parentAge = 50; }
function Child() { }
Child.prototype.__proto__ = Parent.prototype;
const obj = new Child();
console.log(obj.parentAge);
// undefined复制代码



function Parent() { this.parentAge = 50; }
function Child() { }
Child.prototype.__proto__ = new Parent();
const obj = new Child();
console.log(obj.parentAge);    // 50复制代码



function Parent() { this.parentAge = 50; }
function Child() { }
Child.prototype = new Parent();
Child.prototype.constructor = Child;
// 注意重置constructor 
const obj = new Child();
console.log(obj.parentAge);    // 50复制代码



/* 实现一个 new */
function myNew(func, ...args) {
    const obj = {};
    // 新建一个空对象  
    func.call(obj, ...args);
    // 执行构造函数  
    obj.__proto__ = func.prototype;
    // 设置原型链  
    return obj;
}
function Puppy(age) { this.puppyAge = age; }

Puppy.prototype.say = function () { console.log("汪汪汪"); }

const myPuppy3 = myNew(Puppy, 2);

console.log(myPuppy3.puppyAge);
// 2
console.log(myPuppy3.say());
// 汪汪汪复制代码



/* 实现一个instanceof */
function myInstanceof(targetObj, targetClass) {
    // 参数检查 
    if (!targetObj || !targetClass || !targetObj.__proto__ || !targetClass.prototype) {
        return false;
    }
    let current = targetObj;
    while (current) {
        // 一直往原型链上面找   
        if (current.__proto__ === targetClass.prototype) {
            return true;
            // 找到了返回true   
        }
        current = current.__proto__;
    }
    return false;
    // 没找到返回false
}
// 用我们前面的继承实验下
function Parent() { }
function Child() { }
Child.prototype.__proto__ = Parent.prototype;
const obj = new Child();
console.log(myInstanceof(obj, Child));
// true
console.log(myInstanceof(obj, Parent));
// trueconsole.log(myInsta
